#! /usr/bin/env python
# _*_ coding: utf-8 _*_

'''
kastor (kastor@fobos.org.ar)
Under GNU/GPL v2 License
'''

import pygtk
pygtk.require('2.0')
import gtk, gtk.glade
import gobject
import os
import os.path
import gtkhtml2
import utils
import re

_VERSION = "001"
_NAME = "UTwitt"
ARCHIVO_GLADE="utwitt.glade"
dirconfig = os.getenv('HOME') + "/.utwitt"

class mainUT:
	def __init__(self):
		self.app_icon = utils.find_image('utwitt.png')
		self.builder = gtk.Builder()
		self.builder.add_from_file("utwitt.xml")
		self.builder.connect_signals(self)       
		if self.app_icon:
			# if there are no icon files, there is no way to setup the systray 
			self.systray_setup()
	        else:
	 		self.systray = None

		self.ventana = self.builder.get_object("principal")
		self.ventana.set_title(_NAME + " " + _VERSION)
		self.add_roster()
		self.ventana.show()
		message = "Hola este es un msg de prueba de @UTwitt http://utwitt.googlecode.com/"
		model = self.modelo
		''' ejemplo de como se inserta nuevos tweets '''
		self.insert_row(model,None,'Avatar',message)


	def systray_setup(self):
		self.systray = gtk.StatusIcon()
	        self.systray.set_from_file(self.app_icon)
		self.systray.connect('activate', self.systray_cb)
		self.systray.set_tooltip('UTwitt: Click to toggle window visibility.')
		self.systray.set_visible(True)
		return

	def systray_cb(self, widget, user_param=None):
		''' aca hay que reposicionar la ventana '''
        	if self.ventana.get_property('visible'):
		        self.ventana.hide()
	        else:
		        self.ventana.deiconify()
		        self.ventana.present()


	def add_roster(self):
	 	''' fixear el ListStore para que almacene un gtkPixBuf para el avatar '''
		self.roster = self.builder.get_object("roster")
		self.modelo = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
		self.roster.set_model(self.modelo)
		self.roster.set_headers_visible(True)

		renderer = gtk.CellRendererText()
		''' FIX ME WARNING **: expected enumeration type PangoWrapMode, but got GtkWrapMode instead '''
		renderer.set_property('wrap-mode', gtk.WRAP_WORD)
		renderer.set_property('wrap-width', 200)
		renderer.set_property('width', 15)

		col = gtk.TreeViewColumn('avatar',renderer, text=0)
		col.set_resizable(False)
		self.roster.append_column(col)
		col = gtk.TreeViewColumn('msg',renderer, text=1)
		self.roster.append_column(col)
		
		self.roster.show()

	def insert_row(self,model,parent,first,second):
		myiter=self.modelo.insert_after(parent,None)
		model.set_value(myiter,0,first)
		model.set_value(myiter,1,second)
		return myiter

		
	def on_acerca_de_activate(self,window):
		authors = [ 
                          "kastor <kastor@fobos.org.ar>"
                          ]
		dialog = gtk.AboutDialog()
		dialog.set_name("UTwitt")
		dialog.set_website("http://utwitt.googlecode.com/")
		dialog.set_version(_VERSION)
                dialog.set_copyright("Copyleft \xc2\xa9 2009")
                dialog.set_authors  (authors)
		dialog.connect ("response", lambda d, r: d.destroy())
		dialog.show()
			
	def on_principal_destroy(self, widget, data=None):
    		gtk.main_quit()

	def on_salir_activate(self,window):
		gtk.main_quit()

	def on_window_delete_event(self,widget,event,data=None):
		gtk.main_quit()
		return 0
	
					
def main():
	app = mainUT()
	gtk.main()
	return 0

if __name__ == "__main__":
	main()
